package com.frogs.ucenter.controller;

import java.util.Arrays;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.PropertyUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.frogs.framework.base.web.BaseController3;
import com.frogs.framework.util.DWZResponse;
import com.frogs.framework.util.PageList;
import com.frogs.framework.util.RenderUtil;
import com.frogs.framework.web.HttpRequestInfo;
import com.frogs.ucenter.entity.TagBaseInfo;
import com.frogs.ucenter.service.TagBaseInfoService;

@Controller
@RequestMapping("/admin/tagBaseInfo")
public class TagBaseInfoController extends BaseController3 {

	private static final Logger log = LoggerFactory.getLogger(TagBaseInfoController.class);

	
	private static final String HOME_VIEW = "/admin/tagInfo/home";

    private static final String EDIT_VIEW = "/admin/tagInfo/edit";

    @Autowired
    private TagBaseInfoService tagBaseInfoService;

    @RequestMapping()
    public String listTagInfoBean(HttpServletRequest request, ModelMap model) {
    	try {
    		HttpRequestInfo reqInfo = new HttpRequestInfo(request);
	        int status = reqInfo.getIntParameter("status", 1);
	        setRequestModelMap( request, model, true );
	        model.put("status", status);
	        getPageList( model );
    	} catch(Exception e) {
    		log.error("func[listTagInfoBean] request[{}] exception[{} - {}] desc[fail]",
    				new Object[] { request.getParameterMap(), Arrays.deepToString(e.getStackTrace()) });
    	}
        return HOME_VIEW;
    }

    @RequestMapping(EDIT)
    public String createTagInfoBean(HttpServletRequest request, ModelMap model) {
        HttpRequestInfo reqInfo = new HttpRequestInfo(request);
        setRequestModelMap( request, model, false );
        Integer id = reqInfo.getIntParameter( "id", -1 );
        if( id > 0 ){
            model.put( "tagBean",tagBaseInfoService.getById( id ) ); 
        }
        model.put("parentRel", model.get("rel"));
        return EDIT_VIEW;
    }

    @RequestMapping(ADD)
    public String saveTagInfoBean(HttpServletRequest request, HttpServletResponse response, ModelMap model, TagBaseInfo tagInfoBean) {
        setRequestModelMap(request, model, false);
        DWZResponse.Builder builder;
        try {
        	tagInfoBean.setUpdateTime(new Date());
            tagBaseInfoService.insert( tagInfoBean ); 
            builder = DWZResponse.getSucessBuilder("添加成功!");
            builder.navTabId(String.valueOf(model.get("parentRel")));
            builder.callbackType("closeCurrent");
            builder.forwardUrl("/admin/tagBaseInfo");
        } catch (Exception e) {
            builder = DWZResponse.getFailBuilder("新增失败!" + e.getMessage());
        }
        RenderUtil.renderHtml(builder.build().toString(), response);
        return null;
    }

    @RequestMapping(SHOW)
    public String showTagInfoBean(HttpServletRequest request, ModelMap model) {
        HttpRequestInfo reqInfo = new HttpRequestInfo(request);
        Integer id = reqInfo.getIntParameter( "id", -1 );
        if( id > 0 ){
            model.addAttribute( tagBaseInfoService.getById( id ) ); 
        }
        setRequestModelMap(request, model);
        return null;
    }

    @RequestMapping(UPDATE)
    public String updateTagInfoBean(HttpServletRequest request, HttpServletResponse response, ModelMap model, TagBaseInfo tagInfoBeanNew) throws Exception {
        HttpRequestInfo reqInfo = new HttpRequestInfo(request);
        Integer id = reqInfo.getIntParameter( "id", -1 );
        if( id > 0 ){
            DWZResponse.Builder builder;
            try {
            	TagBaseInfo tagInfoBean = tagBaseInfoService.getById( id ); 
                PropertyUtils.copyProperties(tagInfoBean, tagInfoBeanNew);
                tagInfoBean.setUpdateTime(new Date());
                tagBaseInfoService.update( tagInfoBean ); 
                builder = DWZResponse.getSucessBuilder("更新成功！");
                builder.navTabId(reqInfo.getParameter("parentRel"));
                builder.callbackType("closeCurrent");
                builder.forwardUrl("/admin/tagInfo");
                model.clear();
            } catch (Exception e) {
                builder = DWZResponse.getFailBuilder("更新出错！" + e.getMessage());
            }
            RenderUtil.renderHtml(builder.build().toString(), response);
        }
        return null;
    }

    @RequestMapping("/batDel")
    public String deleteTagInfoBean(HttpServletRequest request, HttpServletResponse response, ModelMap model, String ids) {
        HttpRequestInfo reqInfo = new HttpRequestInfo(request);
        setRequestModelMap(request, model);
        DWZResponse.Builder builder;
        try {
            String[] idArray = ids.split(",");
            model.put("inIdList", idArray);
            model.put("newStatus", 0);
            tagBaseInfoService.update(model);
            builder = DWZResponse.getSucessBuilder("删除成功！");
        } catch (Exception e) {
            builder = DWZResponse.getFailBuilder("操作失败" + e.getMessage());
        }
        RenderUtil.renderHtml(builder.build().toString(), response);
        model.clear();
        return null;
    }
    
    @RequestMapping(DELETE)
    public String deleteTagInfoBean(HttpServletRequest request, HttpServletResponse response, ModelMap model, int id) {
    	HttpRequestInfo reqInfo = new HttpRequestInfo(request);
    	setRequestModelMap(request, model);
    	DWZResponse.Builder builder;
    	try {
    		String[] idArray = {String.valueOf(id)};
    		model.put("inIdList", idArray);
    		model.put("newStatus", 0);
    		tagBaseInfoService.update(model);
    		builder = DWZResponse.getSucessBuilder("删除成功！");
    	} catch (Exception e) {
    		builder = DWZResponse.getFailBuilder("操作失败" + e.getMessage());
    	}
    	RenderUtil.renderHtml(builder.build().toString(), response);
    	model.clear();
    	return null;
    }

    private PageList<TagBaseInfo> getPageList(ModelMap model) {
        PageList<TagBaseInfo> tagInfoBeanList = tagBaseInfoService.getList( model, Integer.parseInt(model.get("pageNum").toString()), Integer.parseInt(model.get("numPerPage").toString()) ); 
        model.put("tagInfoBeanList",tagInfoBeanList);
        model.put("pageTurn",tagInfoBeanList.getPageTurn());
        return tagInfoBeanList;
    }
}